package com.fabriciojf.persistence;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Classe para conexao jdbc com o banco de dados
 * utilizada para facilitar a geracao do aar
 * e diminuir as dependencias
 *
 * @author Fabricio fabriciojf@gmail.com
 * @since 03/05/2010
 * @version 1.0
 */
public class DatabaseLocator {

    /**
     * Singleton
     */
    private static DatabaseLocator instance = new DatabaseLocator();

    public static DatabaseLocator getInstance() {
        return instance;
    }

    /**
     * Retorna a conexao com o banco de dados
     * 
     * @return
     * @throws java.sql.SQLException
     * @throws java.lang.ClassNotFoundException
     */
    public Connection getConnection() throws SQLException, ClassNotFoundException {
        Connection conn = null;

        Class.forName("org.postgresql.Driver");
        conn = DriverManager.getConnection(
                "jdbc:postgresql://localhost/teste", "postgres", "masterkey");

        return conn;
    }

    /**
     * Fecha os recusros alocados para a conexao
     * 
     * @param conn
     * @param st
     */
    public void closeResources(Connection conn, Statement st) {
        try {
            if (st != null) {
                st.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
        }
    }

    /**
     * Duplicidade de codigo para teste no hudson
     * @param conn
     * @param st
     */
    public void closeResourcesHudson(Connection conn, Statement st) {
        try {
            if (st != null) {
                st.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
        }
    }
}










